package com.samphanie.dries.exception;

import lombok.var;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.zalando.problem.Problem;
import org.zalando.problem.ProblemBuilder;
import org.zalando.problem.StatusType;
import org.zalando.problem.spring.web.advice.ProblemHandling;

import java.net.URI;

/**
 * @Description:
 * @Author ZSY
 * @createTime 2021/2/25 16:16
 */
@ControllerAdvice
public class ExceptionHandler implements ProblemHandling {

    // DEBUG 模式开启   问题的因果链默认情况下处于禁用状态.如果需要，可以将其覆盖
    //    @Override
    //    public boolean isCausalChainsEnabled() {
    //        return true;
    //    }


    @Override
    public ProblemBuilder prepare(Throwable throwable, StatusType status, URI type) {

        var exception = (MissingServletRequestParameterException) throwable;
        return Problem.builder()
                .withTitle(status.getReasonPhrase())
                .withStatus(status)
                .withDetail(exception.getMessage())
                .with("parameter", exception.getParameterName());
    }
}
